package com.example.camerapush;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.example.camerapush.ptp.Camera;
import com.example.camerapush.ptp.model.ObjectInfo;
import com.lsh.XXRecyclerview.CommonRecyclerAdapter;
import com.lsh.XXRecyclerview.CommonViewHolder;
import com.yanzhenjie.fragment.NoFragment;

import java.util.ArrayList;

/**
 * Author:lsh
 * Version: 1.0
 * Description:
 * Date: 2018/2/20
 */

public class PictureAllFragment extends NoFragment {
    RecyclerView rv;
    MainActivity mainActivity;
    private ArrayList<Integer> mDatas;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.gallery_frag, container, false);
        rv = (RecyclerView) view.findViewById(R.id.rv);
//        return super.onCreateView(inflater, container, savedInstanceState);
        mainActivity = ((MainActivity) getActivity());
        initView();
        registercheckBroadCast();
        return view;
    }

    private void registercheckBroadCast() {
        IntentFilter checkFilter = new IntentFilter();
        checkFilter.addAction(mainActivity.CHECKUPFILEACTION);
        mainActivity.registerReceiver(checkUpNumberReceiver, checkFilter);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mainActivity.unregisterReceiver(checkUpNumberReceiver);
    }

    private BroadcastReceiver checkUpNumberReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            switch (action) {
                case MainActivity.CHECKUPFILEACTION:
                    if (mainActivity.handles == null || mainActivity.handles.length == 0)
                    finish();
                    break;
            }
        }
    };

    private void initView() {
        if (mainActivity.handles == null) {
            mainActivity.initPtp();
            return;
        }

        mDatas = new ArrayList();
        for (int handle : mainActivity.handles) {
            mDatas.add(handle);
        }
        rv.setLayoutManager(new GridLayoutManager(mainActivity, 3));
        final CommonRecyclerAdapter<Integer> adapter = new CommonRecyclerAdapter<Integer>(mainActivity, mDatas, R.layout.rv_item) {
            @Override
            public void convert(final CommonViewHolder commonViewHolder, Integer integer, int i, boolean b) {
                if (mainActivity.camera == null) return;
                mainActivity.camera.retrieveImageInfo(new Camera.RetrieveImageInfoListener() {
                    @Override
                    public void onImageInfoRetrieved(int objectHandle, ObjectInfo objectInfo, final Bitmap thumbnail) {
                        mainActivity.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                ImageView iv = commonViewHolder.getView(R.id.iv);
                                ViewGroup.LayoutParams params = iv.getLayoutParams();
                                params.height = mainActivity.dm.widthPixels / 3;
                                params.width = mainActivity.dm.widthPixels / 3;
                                iv.setLayoutParams(params);
                                iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
                                iv.setImageBitmap(thumbnail);
                            }
                        });
                    }
                }, integer);
            }
        };
        rv.setAdapter(adapter);
        adapter.setOnItemClickListener(new CommonRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClickListener(CommonViewHolder commonViewHolder, int i) {
                Bundle bundle = new Bundle();
                bundle.putIntegerArrayList("handles", mDatas);
                bundle.putInt("index", i);
                NoFragment fragment = fragment(BigPicFragment.class, bundle);
                startFragment(fragment);
            }
        });


    }
}
